package cn.hpclub.server.cfg;

import org.beetl.core.GroupTemplate;
import org.beetl.ext.jfinal.BeetlRenderFactory;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.plugin.tablebind.AutoTableBindPlugin;
import com.jfinal.ext.plugin.tablebind.SimpleNameStyles;
import com.jfinal.ext.route.AutoBindRoutes;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;

import cn.dreampie.shiro.core.ShiroPlugin;
import cn.hpclub.server.restful.RestKit;
import cn.hpclub.server.security.MyJdbcAuthzService;
import cn.hpclub.server.security.MyShiroInterceptor;
import cn.hpclub.server.security.ShiroExt;

public class Config extends JFinalConfig{

    public static void main(String[] args){
        String webAppDir = Config.class.getResource("/").getPath().replace("/WEB-INF/classes/", "");
        int port = 8090;
        String content = "/";
        if(args != null && args.length > 0){
            try{
                port = Integer.parseInt(args[0]);
                content = args[1];
            }
            catch(Exception e){}
        }
        try{
            JFinal.start(webAppDir.substring(1), port, content, 5);
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 供Shiro插件使用。
     */
    Routes routes;

    @Override
    public void configConstant(Constants me){
        me.setErrorView(401, "/401.html");
        me.setErrorView(403, "/403.html");
        me.setError404View("/404.html");
        me.setError500View("/500.html");

        // 加载数据库配置文件
        loadPropertyFile("jdbc.properties");
        // 设定Beetl
        me.setMainRenderFactory(new BeetlRenderFactory());
        // 设定为开发者模式
        me.setDevMode(true);
        me.setMaxPostSize(1024 * 1024 * 20);
        // Beetl
        GroupTemplate gt = BeetlRenderFactory.groupTemplate;
        gt.registerFunctionPackage("so", new ShiroExt());

        // ActivitiesController.hostPrefix =
        // PropKit.use("serverConfig.properties").get("hostPrefix");
    }

    @Override
    public void configRoute(Routes me){
        this.routes = me;
        // 配置路由，三个参数：访问路径（API版本），jfinal路由对象，要扫描的包(包下加了API注解的controller会被扫描)
        RestKit.buildRoutes("/api/v1", me, "cn.hpclub.server.controller.client");
        me.add(new AutoBindRoutes());
    }

    @Override
    public void configPlugin(Plugins me){
        // mysql
        String url = getProperty("jdbcUrl");
        String username = getProperty("user");
        String password = getProperty("password");
        String driverClass = getProperty("driverClass");
        String filters = getProperty("filters");

        // mysql 数据源
        DruidPlugin dsMysql = new DruidPlugin(url, username, password, driverClass, filters);
        dsMysql.setMaxActive(200);
        me.add(dsMysql);

        ActiveRecordPlugin arpMysql = new ActiveRecordPlugin("mysql", dsMysql);
        me.add(arpMysql);

        AutoTableBindPlugin atbp = new AutoTableBindPlugin(dsMysql, SimpleNameStyles.LOWER);
        atbp.setShowSql(true);
        atbp.setDialect(new MysqlDialect());// 配置MySql方言
        me.add(atbp);

        // 加载Shiro插件
        me.add(new ShiroPlugin(routes, new MyJdbcAuthzService()));
    }

    @Override
    public void configInterceptor(Interceptors me){
        me.add(new MyShiroInterceptor());
    }

    @Override
    public void configHandler(Handlers me){
        // 配置handler
        RestKit.buildHandler(me);

    }

    public void afterJFinalStart(){
        // SerialNumberUtil.lastSnNumberInit();
    }

}
